﻿using Langchur.Controllers;
using Helper;
using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Web; 
using System.Data;
using System.Xml.Linq;

namespace Langchur.ApiFilter
{
    /// <summary>
    /// UploadFiles 的摘要说明
    /// </summary>
    public class UploadFiles : IHttpHandler
    { 
         

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                context.Response.ContentType = "application/json";
                String UpFolder = DateTime.Now.ToString("yyyy-MM-dd");
                String RelativePath = ""; 
                List<String> NewFiles = new List<string>();


                for (Int32 i = 0; i < context.Request.Files.Count; i++)
                {


                    // 自定义文件名
                    String FileName = Guid.NewGuid().ToString();

                    // 原图片名称
                    String _FileName = context.Request.Files[i].FileName;
                    String lastName = _FileName.Split('.')[_FileName.Split('.').Length - 1];

                    // 文件存放的文件夹
                    String Folder = "";


                    if (context.Request.QueryString["path"] != null && context.Request.QueryString["name"] != null)
                    {
                        Folder = context.Request.QueryString["path"];
                        FileName = context.Request.QueryString["name"]; 
                    }
                    else
                    {
                        Folder = "/UploadFiles/" + UpFolder + "/" + lastName;
                    } 

                    // 验证文件夹路径是否存在，不存在则创建该路径
                    if (!System.IO.Directory.Exists(context.Server.MapPath(Folder)))
                    {
                        System.IO.Directory.CreateDirectory(context.Server.MapPath(Folder));
                    }
                    // 保存文件
                    RelativePath = Folder + "/" + FileName + "." + lastName;
                    String FullPath = context.Server.MapPath(RelativePath);
                    context.Request.Files[i].SaveAs(FullPath);

                    if (lastName.ToLower() == "png" || lastName.ToLower() == "jpg")
                    {
                        //压缩文件
                        String Express = Folder + "/" + Guid.NewGuid().ToString() + "." + lastName;
                        ImgHelper.imageZoom(context.Server.MapPath(RelativePath), context.Server.MapPath(Express), 1);
                        NewFiles.Add(Express);
                        try
                        {
                            System.IO.File.Delete(context.Server.MapPath(RelativePath));
                        }
                        catch
                        {

                        }
                    }
                    else
                    {
                        NewFiles.Add(RelativePath);
                    }
                } 

                if (NewFiles.Count > 0)
                {
                    String Rsp = "";
                    foreach (String s in NewFiles)
                    {
                        Rsp += "{\"file\":\"" + s + "\"},";
                    }
                    Rsp = "[" + Rsp.TrimEnd(',') + "]";
                    context.Response.Write(Rsp);
                }
                else
                {
                    context.Response.Write("{}");
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}